package com.itheima.a04xpathdemo;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.util.List;

public class XPathDemo2 {
    public static void main(String[] args) throws DocumentException {
        //1.创建一个解析器对象
        SAXReader saxReader = new SAXReader();
        //2.解析xml文件，并且返回文档对象
        Document document = saxReader.read(new File("sockethomework\\src\\com\\itheima\\a04xpathdemo\\person.xml"));
        //3.利用XPath方式来进行快速检索
        //两个方法
        //1.检索单个   document.selectSingleNode("路径")
        //2.检索多个   document.selectNodes("路径")

        //利用相对路径进行检索
        //参照物：自己本身
        Element rootElement = document.getRootElement();
        /*Element age = (Element) rootElement.selectSingleNode("./person/age");
        System.out.println(age.getText());*/

        List<Element> list = rootElement.selectNodes("./person/age");
        for (Element element : list) {
            System.out.println(element.getText());
        }
    }
}
